package bsh;

import org.objectweb.asm.MethodVisitor;

import bsh.asm.acessor.ObjectAccessor;
import bsh.exception.CompilationAssertionFailed;
import bsh.meta.MetaTypeWrapper;
import bsh.meta.TypeWrapper;

/**
 * ObjectResovler para class literals
 * 
 * @author takeshi
 */
public class ClassResolver implements ObjectAccessor {

    private final TypeWrapper wrapped;

    /**
     * @param type {@link TypeWrapper}
     */
    public ClassResolver(TypeWrapper type) {
        this.wrapped = type;
    }

    /**
     * {@inheritDoc}
     */
    public void loadObject(MethodVisitor visitor, CompilationState state) {
        state.pushStack(this.type());
    }

    /**
     * {@inheritDoc}
     */
    public void preStore(MethodVisitor visitor, CompilationState state) {
    }

    /**
     * {@inheritDoc}
     */
    public boolean readOnly() {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public void storeObject(MethodVisitor visitor, CompilationState state) {
        throw new CompilationAssertionFailed("cannot store on a class literal");
    }

    /**
     * {@inheritDoc}
     */
    public TypeWrapper type() {
        return new MetaTypeWrapper(this.wrapped);
    }

    /**
     * {@inheritDoc}
     */
    public TypeWrapper getWrapped() {
        return this.wrapped;
    }

}
